package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Department;
import unipi.database.ErrorCodes;
import unipi.database.User;
import unipi.helpers.HTMLPages;
import unipi.helpers.HTMLParts;
import unipi.helpers.Validator;

import java.util.List;

public class Registration  extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public Registration()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}	
	
	private String validateInputs(String name, String surname, String email, String department, String username, String password, String passwordConfirm)
	{
		if(!Validator.isValid(name) || !Validator.isValid(surname) || !Validator.isValid(department) || 
		   !Validator.isValid(username) || !Validator.isValid(password) ||!Validator.isValid(passwordConfirm))
		{
			return "Οι χαρακτήρες ', \" και & δεν επιτρέπονται στα πεδία εισόδου.";
		}
		
		if(!Validator.isEmail(email))
		{
			return "Το email που εισάγατε δεν είναι έγκυρο.";
			
		}
		
		return null;		
	}
	
	private String getPage(List<List<String>> deps, String errorMsg)
	{
		String title = "Εγγραφή";
		String css = HTMLParts.getCSSLink("css/registration.css") + HTMLParts.getCSSLink("css/properties.css");
		String select = HTMLPages.getDropDownList(deps, "department", "none", "--Επιλέξτε Τμήμα--"); 
		
		String html = HTMLParts.DOCTYPE + "\n<html xmlns=\"http://www.w3.org/1999/xhtml\">";
		
		html += HTMLParts.getHead(title, css, "");			
		
		html += "<body>" +
			"<div id=\"regWrap\">" +
				"<div id=\"regMain\">" +
					"<div id=\"title\">Φόρμα Αίτησης</div>" +
					"<div id=\"regFields\"> " +
					"<span>Όνομα:</span> " +
					"<span>Επώνυμο:</span> " + 
					"<span>Email:</span> " + 
					"<span>Τμήμα:</span> " +
					"<span>Όνομα Χρήστη:</span> " + 
					"<span>Κωδικός:</span> " +
					"<span>Επαλήθευση Κωδικού:</span> " +
					"</div>" +
					"<div id=\"regValues\">" +
						"<form method=\"post\" action=\"Registration\">" +
							"<input name=\"name\" type=\"text\" value=\"\" /> " +
							"<input name=\"surname\" type=\"text\" value=\"\" /> " +
							"<input name=\"email\" type=\"text\" value=\"\" />" +
							"<div class=\"ddldivstyle\">"+ select +"</div>" +
							"<input name=\"username\" type=\"text\" value=\"\" /> " +
							"<input name=\"password\" type=\"password\" value=\"\" /> " +
							"<input name=\"passwordConfirm\" type=\"password\" value=\"\" /> " +
							"<input name=\"Submit\" type=\"submit\" value=\"Υποβολή Αίτησης\" />" +
						"</form>" + "<div class=\"info\" style=\"clear:both\">"+ errorMsg + "</div>" +
					"</div>" +
				"</div>" +
			"</div>" +
		"</body>" +
		"</html>";		

		return html;
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String surname = request.getParameter("surname");
		String email = request.getParameter("email");
		String department = request.getParameter("department");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String passwordConfirm = request.getParameter("passwordConfirm");
		
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();	
		
		List<List<String>> deps = Department.getDpts();
		
		String validation = validateInputs(name, surname, email, department, username, password, passwordConfirm);
		
		if(validation == null)
		{				
			boolean passcheck = password.equals(passwordConfirm);
			boolean depcheck = !department.equals("none");
			
			if (passcheck && depcheck)
			{		
				ErrorCodes addUser = User.Add(name, surname, email, username, password, department);
				
				if(addUser == ErrorCodes.OK)
					response.sendRedirect("Login");			
				else if(addUser == ErrorCodes.duplicateUsernames)
					out.println(getPage(deps, "Duplicate Usernames!"));
				else
					out.println(getPage(deps, "Database Error"));				
			}
			else
				if(!passcheck)			
					out.println(getPage(deps, "Password confirm doesn't match!"));	
				else
					out.println(getPage(deps, "You didn't select a department!"));		
		}
		else
			out.println(getPage(deps, validation));
		
		out.close();
		
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();		

		List<List<String>> deps = Department.getDpts();
		
		out.println(getPage(deps, ""));		
		
		out.close();

	}
	
}
